package com.tianqu.tms.www.car.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.MarketCarInfoDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.Vendor;
import com.tianqu.tms.www.car.actionform.CarStockModifyForm;
import com.tianqu.tms.www.car.viewhelper.CarStockModifyViewhelper;

public class CarStockModifyController extends TmsController {
	private Logger logger = Logger.getLogger(CarStockModifyController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			CarStockModifyViewhelper viewhelper = new CarStockModifyViewhelper();
			CarStockModifyForm form = (CarStockModifyForm) uc.getInputForm();
			Car car = Car.getInstance();
			String vin = form.getVin();
			String id = form.getId();
			String licensePlate = form.getLicensePlate();
			
			//
			List<MarketCarInfoDto> list = new ArrayList<MarketCarInfoDto>();
			if(!StringUtil.isNullOrBlank(licensePlate)){
				list = car.findCarListNotKeep(licensePlate, 2); // 车辆库存状态（不为4：存档）modify by mufeiyu
			}else if(!StringUtil.isNullOrBlank(vin)){
				list = car.findCarListNotKeep(vin, 1); // 车辆库存状态（不为4：存档）modify by mufeiyu
			}else if(!StringUtil.isNullOrBlank(id)){
				list = car.findCarListNotKeep(id, 3); // 车辆库存状态（不为4：存档）modify by mufeiyu
			}
			
			//
			if(list.size() > 1){
				int i = 0;
				if (list.size() > 0 && list != null) {
					for (MarketCarInfoDto dto : list) {
						dto.setNum(++i);
						if(i%2 == 0) dto.setCssFlag(2); 
						else dto.setCssFlag(1);						
					}
				}
				viewhelper.setConfirmFlag(3);
				viewhelper.setMarkCarInfoDtoList(list);
				uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			}else if(list.size() == 1){//找到一条
				viewhelper.setConfirmFlag(2);
				setDetailInfo(list.get(0),viewhelper);
				uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			}else{//没有找到数据
				viewhelper.setConfirmFlag(1);
				uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			}
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

	private void setDetailInfo(MarketCarInfoDto car,CarStockModifyViewhelper viewhelper) throws TmsDaoException{
		viewhelper.setId(car.getId());
		viewhelper.setSerial(car.getSerial());
		viewhelper.setLicensePlate(car.getLicensePlate());
		viewhelper.setSerial(car.getSerial());
		viewhelper.setDisplacement(String.valueOf(car.getDisplacement()));
		viewhelper.setGearbox(String.valueOf(car.getGearbox()));
		viewhelper.setVendorId(car.getVendorId());
		viewhelper.setBrandId(car.getBrandId());
		viewhelper.setCarStyleId(car.getCarStyleId());
		viewhelper.setCarStyle(car.getCarStyleId());
		viewhelper.setRegDate(car.getRegDate());
		viewhelper.setSendCardDate(car.getSendCardDate());
		viewhelper.setCarScore(car.getCarScore());
		viewhelper.setImg1(car.getPhoto_1());
		viewhelper.setImg2(car.getPhoto_2());
		viewhelper.setImg3(car.getPhoto_3());
		viewhelper.setImg4(car.getPhoto_4());
		viewhelper.setImg5(car.getPhoto_5());
		viewhelper.setImg6(car.getPhoto_6());
		viewhelper.setImg7(car.getPhoto_7());
		viewhelper.setImg8(car.getPhoto_8());
		viewhelper.setCarOwner(car.getCarOwner());
		viewhelper.setTempLicensePlate(car.getTempLicensePlate());
		viewhelper.setColor(String.valueOf(car.getColor()));
		viewhelper.setJqx(car.getJqx());
		viewhelper.setYearCarefulPeriod(car.getYearCarefulPeriod());
		viewhelper.setCompanyContactPerson(car.getCompanyContactPerson());
		viewhelper.setCompanyContactPhone(car.getCompanyContactPhone());
		viewhelper.setRemark(car.getRemark());
		viewhelper.setAttestStatus(car.getAttestStatus());
		viewhelper.setDetectRemark(car.getDetectRemark());
		viewhelper.setInternalScore(car.getInternalScore());
		viewhelper.setExternalScore(car.getExternalScore());
		viewhelper.setVin(car.getVin());
		viewhelper.setVinNum(car.getVin());
		if(car.getPrice() == 0) viewhelper.setPrice("");
		else viewhelper.setPrice(String.valueOf(car.getPrice()));
		 
		if(car.getMileage() == 0) viewhelper.setMileage("");
		else viewhelper.setMileage(String.valueOf(car.getMileage()));
		 
		viewhelper.setMemberName(car.getMemberName());			
		viewhelper.setMemberId(String.valueOf(car.getMemberId()));
		viewhelper.setIsMatch(car.getIsMatch());
		viewhelper.setCarName(car.getCarName());
		viewhelper.setIsShelve(car.getIsShelve());
		
		CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
		// 车辆品牌
		Vendor vendor = Vendor.getInstance();
		if(!StringUtil.isNullOrBlank(viewhelper.getVendorId())){
			if("V".equals(viewhelper.getVendorId().substring(0,1))){
				viewhelper.setFromType(2);
				viewhelper.setVendorList(commercialVehicle.findAllVendorInfo());
			}else if("S".equals(viewhelper.getVendorId().substring(0,1))){
				viewhelper.setFromType(1);
				viewhelper.setVendorList(vendor.findAllVendorInfo());
			}
		}
		
		// 亮点配置
		String strConfigKye = car.getConfigKey();
		if (null != strConfigKye && !"".equals(strConfigKye)) {
			String[] aa = strConfigKye.split(",");
			if (null != aa) {
				if (aa.length >= 1 && "1".equals(aa[0])) viewhelper.setLdpz1(true);
				if (aa.length >= 2 && "1".equals(aa[1])) viewhelper.setLdpz2(true);
				if (aa.length >= 3 && "1".equals(aa[2])) viewhelper.setLdpz3(true);
				if (aa.length >= 4 && "1".equals(aa[3])) viewhelper.setLdpz4(true);
				if (aa.length >= 5 && "1".equals(aa[4])) viewhelper.setLdpz5(true);
				if (aa.length >= 6 && "1".equals(aa[5])) viewhelper.setLdpz6(true);
				if (aa.length >= 7 && "1".equals(aa[6])) viewhelper.setLdpz7(true);
				if (aa.length >= 8 && "1".equals(aa[7])) viewhelper.setLdpz8(true);
				if (aa.length >= 9 && "1".equals(aa[8])) viewhelper.setLdpz9(true);
				if (aa.length >= 10 && "1".equals(aa[9])) viewhelper.setLdpz10(true);
				if (aa.length >= 11 && "1".equals(aa[10])) viewhelper.setLdpz11(true);
				if (aa.length >= 12 && "1".equals(aa[11])) viewhelper.setLdpz12(true);
				if (aa.length >= 13 && "1".equals(aa[12])) viewhelper.setLdpz13(true);
				if (aa.length >= 14 && "1".equals(aa[13])) viewhelper.setLdpz14(true);
				if (aa.length >= 15 && "1".equals(aa[14])) viewhelper.setLdpz15(true);
				if (aa.length >= 16 && "1".equals(aa[15])) viewhelper.setLdpz16(true);
				if (aa.length >= 17 && "1".equals(aa[16])) viewhelper.setLdpz17(true);
				if (aa.length >= 18 && "1".equals(aa[17])) viewhelper.setLdpz18(true);
				if (aa.length >= 19 && "1".equals(aa[18])) viewhelper.setLdpz19(true);
				if (aa.length >= 20 && "1".equals(aa[19])) viewhelper.setLdpz20(true);
				if (aa.length >= 21 && "1".equals(aa[20])) viewhelper.setLdpz21(true);
				if (aa.length >= 22 && "1".equals(aa[21])) viewhelper.setLdpz22(true);
				if (aa.length >= 23 && "1".equals(aa[22])) viewhelper.setLdpz23(true);
				if (aa.length >= 24 && "1".equals(aa[23])) viewhelper.setLdpz24(true);
				if (aa.length >= 25 && "1".equals(aa[24])) viewhelper.setLdpz25(true);
				if (aa.length >= 26 && "1".equals(aa[25])) viewhelper.setLdpz26(true);
				if (aa.length >= 27 && "1".equals(aa[26])) viewhelper.setLdpz27(true);
				if (aa.length >= 28 && "1".equals(aa[27])) viewhelper.setLdpz28(true);
				if (aa.length >= 29 && "1".equals(aa[28])) viewhelper.setLdpz29(true);
				if (aa.length >= 30 && "1".equals(aa[29])) viewhelper.setLdpz30(true);
				if (aa.length >= 31 && "1".equals(aa[30])) viewhelper.setLdpz31(true);
				if (aa.length >= 32 && "1".equals(aa[31])) viewhelper.setLdpz32(true);
				if (aa.length >= 33 && "1".equals(aa[32])) viewhelper.setLdpz33(true);
				if (aa.length >= 34 && "1".equals(aa[33])) viewhelper.setLdpz34(true);
				if (aa.length >= 35 && "1".equals(aa[34])) viewhelper.setLdpz35(true);
				if (aa.length >= 36 && "1".equals(aa[35])) viewhelper.setLdpz36(true);
				if (aa.length >= 37 && "1".equals(aa[36])) viewhelper.setLdpz37(true);
				if (aa.length >= 38 && "1".equals(aa[37])) viewhelper.setLdpz38(true);
				if (aa.length >= 39 && "1".equals(aa[38])) viewhelper.setLdpz39(true);
				if (aa.length >= 40 && "1".equals(aa[39])) viewhelper.setLdpz40(true);
			}
		}
	}
}
